package com.softeem.xyblogmaster.util;

import cn.hutool.json.JSONUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.io.PrintWriter;


@Data
public class AjaxResult {
    private int code;
    private String message;
    private Object data;


    public AjaxResult(int code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public AjaxResult() {
    }

    public static AjaxResult ok(String message){
        return new AjaxResult(200,message,"");
    }

    public static AjaxResult ok(String message,Object data){

        return new AjaxResult(200,message,data);
    }

    public static AjaxResult fail(int code,String message){
        return new AjaxResult(code,message,"");
    }

    public static AjaxResult fail(String message){
        return new AjaxResult(500,message,
                "");
    }

    public static AjaxResult fail(String message,Object data){
        return new AjaxResult(500,message,data);
    }

    public  void print(HttpServletResponse response) throws IOException {
        response.setHeader("Content-Type","application/json;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        writer.print(JSONUtil.toJsonStr(this));
        writer.flush();
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
